package com.lzh.order.controller;


import com.lzh.commonutils.CommonResult;
import com.lzh.order.service.TPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author lizuhu
 * @since 2021-05-29
 */
@CrossOrigin
@RestController
@RequestMapping("/order/payLog")
public class TPayLogController {
    @Autowired
    private TPayLogService tPayLogService;

    /**
     * 根据订单号创建支付二维码
     * @param orderNo
     * @return
     */
    @GetMapping("/createPayCode")
    public CommonResult createPayCode(@RequestParam String orderNo) {
       Map map = tPayLogService.createPayCode(orderNo);
       return CommonResult.ok().data("payCode",map);
    }

    /**
     * 查看订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayStatus")
    public CommonResult checkPayStatus(@RequestParam String orderNo) {
       Map<String,String> map = tPayLogService.checkPayStatus(orderNo);
       //如果map == null 则是支付失败
       if (map == null) {
           return CommonResult.error().message("支付失败！");
       }
       //如果map.get("trade_state").equals("SUCCESS")则支付成功，并修改订单状态
       if (map.get("trade_state").equals("SUCCESS")) {
            tPayLogService.updateOrderStatus(map);
            return CommonResult.ok().message("支付成功！");
       }
       return CommonResult.ok().message("支付中！");
    }
}

